/*
 * Copyright (C) 2023 HiHope Open Source Organization .
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 *
 * limitations under the License.
 */
#include <stdio.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"

#include "net_config.h"
#include "pzcar_i2c_ctrl.h"
#include "pzcar_led_ctrl.h"
#include "pzcar_wheel_ctrl.h"
#include "pzcar_button_ctrl.h"
#include "pzcar_display_ctrl.h"
#include "pzcar_unisound_ctrl.h"

PzCarInfo g_carInfo = {
    .e_CM = eMode_Max,
    .e_ST = eSt_Stop,
    .e_NetST = Net_ST_DisCon,
    .e_NetMode = Net_MODE_NET_MAX,
    .netCtrlTaskId = NULL,
    .udpServRecvTaskId = NULL,
    .tcpServRecvTaskId = NULL,
    .apInited = 0,
    .staInited = 0,
    .d_speed_curr = 2400,
    .d_speed_def  = 1200,
    .d_speed_min  = 0,
    .d_speed_max  = 3600,
    .d_duration   = 0,
    .d_duration_def = 500,
};

static void MainTask(void)
{
    printf("%s: Begin: PzCarInfo[%d]Bytes\n", __func__, sizeof(PzCarInfo));
    hi_watchdog_disable();

    int ret = I2C0Init();
    if (ret != 0) {
        printf("%s: End. I2C0Init NG!\n", __func__);
        return;
    }

    osThreadId_t g_LedTaskId = LedTaskEntry();
    osThreadId_t g_BtnTaskId = ButtonTaskEntry();
    osThreadId_t g_DispTaskId = DisplayTaskEntry();
    osThreadId_t g_WheelTaskId = WheelTaskEntry();
    osThreadId_t g_UniTaskId = UnisoundTaskEntry();

    while (1) {
        #if 0
        if (g_carInfo.e_CM == eMode_AudCmd) {
            if (g_UniTaskId == NULL) {
                g_UniTaskId = UnisoundTaskEntry();
            }
        }
        // 如果打开这句，当退出语音控制模式时，会关闭UnisoundTask
        // 后台不再支持语音唤醒该模式
        else if (g_UniTaskId != NULL) {
            osThreadTerminate(g_UniTaskId);
            g_UniTaskId = NULL;
        }
        #endif

        if (g_carInfo.e_CM == eMode_NetCtrl) {
            if (g_carInfo.netCtrlTaskId == NULL) {
                g_carInfo.netCtrlTaskId = NetCtrlTaskEntry();
            }
        }
        usleep(1000*1000);
    }

    printf("%s: End.\n", __func__);
    printf("===================================================\n");
#if 0
    osThreadTerminate(g_LedTaskId);
    osThreadTerminate(g_BtnTaskId);
    osThreadTerminate(g_DispTaskId);
    osThreadTerminate(g_WheelTaskId);
    osThreadTerminate(g_UniTaskId);
    I2C0DeInit();
#endif
}

static void MainTaskEntry(void)
{
    //osPriorityAboveNormal[32], osPriorityNormal[24]
    //{.name, .attr_bits, .cb_mem, .cb_size, .stack_mem, .stack_size, .priority, .tz_module, .reserved}
    osThreadAttr_t attr = {"MainTask", 0, NULL, 0, NULL, 1024*4, 24, 0, 0};

    printf("\n\n");
    printf("===================================================\n");
    printf("[MainTaskEntry] create %s:\n", attr.name);
    if (osThreadNew((osThreadFunc_t)MainTask, NULL, &attr) == NULL) {
        printf("[MainTaskEntry] create %s Failed!\n", attr.name);
    }
}
SYS_RUN(MainTaskEntry);